<?php

/**
 * 管理员信息
 *
 * @author suning <snsnsky@gmail.com>
 */

use \Common\Bridge;

define('SITE_AUTH', 'admin-log');
require('../../../common.inc.php');

class adminlog extends \lib\core\AdminAction
{
    const TABLE = 'AdminLog';

    public function doDefault()
    {
        $smarty = $this->app->page();

        $user_id = intval($_GET['user_id']);
        $user_name = trim($_GET['user_name']);
        $item_id = intval($_GET['item_id']);
        $menu_key = trim($_GET['menu_key']);
        $log_flag = trim($_GET['log_flag']);
        $start_time = trim($_GET['start_time']);
        $end_time = trim($_GET['end_time']);
        $page = max(1, intval($_GET['page']));

        $where = array();
        if (!empty($user_id))
            $where['UserId'] = $user_id;
        if (!empty($menu_key))
            $where['MenuKey'] = $menu_key;
        if (!empty($user_name))
            $where['UserName'] = $user_name;
        if (!empty($item_id))
            $where['ItemId'] = $item_id;
        if (!empty($log_flag))
            $where['LogFlag'] = $log_flag;
        if (!empty($start_time))
            $where['CreateTime >='] = $start_time;
        if (!empty($end_time))
            $where['CreateTime <='] = $end_time;

        $pdo_r = Bridge::pdo(Bridge::DB_ADMIN_R);
        $total = $pdo_r->clear()->select('count(LogId)')->from(self::TABLE)->where($where)->getValue();
        $distotal = $pdo_r->clear()->select('count(DISTINCT(ItemId))')->from(self::TABLE)->where($where)->getValue();
        if ($total > 0)
        {
            $data = $pdo_r->clear()->select('*')->from(self::TABLE)->where($where)->order('LogId DESC')->limit(($page - 1) * self::PAGE_SIZE, self::PAGE_SIZE)->getAll();
            $smarty->value('data', $data);

            $pagination = $this->pagination($total, self::PAGE_SIZE, $page);
            $smarty->value('pagination', $pagination);
        }

        $smarty->value('user_id', $user_id);
        $smarty->value('menu_key', $menu_key);
        $smarty->value('user_name', $user_name);
        $smarty->value('item_id', $item_id);
        $smarty->value('log_flag', $log_flag);
        $smarty->value('start_time', $start_time);
        $smarty->value('end_time', $end_time);
        $smarty->value('total', $total);
        $smarty->value('distotal', $distotal);
        $smarty->output('admin/adminlog.tpl');
    }
}

$app->run();